package com.hexinfo.dmpro.sparing.async;

import cn.hutool.core.util.StrUtil;
import com.hexinfo.dmpro.sparing.model.HotStandbyTable;
import com.hexinfo.dmpro.sparing.service.TableSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class AsyncTableTask {

    private final TableSyncService tableSyncService;

    /**
     * 热备-表级同步(异步)
     */
    @Async
    public void tableSync(String sourceName,String rangeStartDate,String rangeEndDate) {
        try{
            if (StrUtil.isBlank(sourceName)){
                sourceName = "无源";
            }
            Boolean aBoolean1 = tableSyncService.tableSync(sourceName, rangeStartDate, rangeEndDate);
        }catch (Exception e){
            log.error("--------------------------------热备-表级同步(异步)异常",e);
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 热备-表级同步（失败表重跑）(异步)
     */
    @Async
    public void failTableRetry(String sourceName,String taskDate) {
        try{
            if (StrUtil.isBlank(sourceName)){
                sourceName = "无源";
            }
            Boolean aBoolean1 = tableSyncService.failTableRetry(sourceName,taskDate);
        }catch (Exception e){
            log.error("--------------------------------热备-表级同步（失败表重跑）(异步)异常",e);
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 热备-表级同步（失败表重跑）(手动重跑)
     */
    public boolean failTableRetryTable(List<HotStandbyTable> hotStandbyTables) {
        try{
            return tableSyncService.failTableRetryTable(hotStandbyTables);
        }catch (Exception e){
            log.error("--------------------------------热备-表级同步（失败表重跑）(异步)异常",e);
            log.error(e.getMessage(),e);
            return false;
        }
    }
}
